﻿package com.oet.web.freemarker;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.springframework.web.servlet.ModelAndView;



import freemarker.cache.FileTemplateLoader;
import freemarker.template.Configuration;
import freemarker.template.TemplateException;

public class TaskJob {
	public static Logger log = Logger.getLogger(TaskJob.class);
//	private YdkSimWebFrontEndService ydkSimFrontEndService;
//
//	public YdkSimWebFrontEndService getYdkSimFrontEndService() {
//		return ydkSimFrontEndService;
//	}
//
//	public void setYdkSimFrontEndService(
//			YdkSimWebFrontEndService ydkSimFrontEndService) {
//		this.ydkSimFrontEndService = ydkSimFrontEndService;
//	}
	public static final String regexString = "doImage.do[?]fileName=book[\\]";
	public static final Pattern regex = Pattern.compile("doImage.do.*?fileName=book\\\\");
	//Delete the String ";jsessionid=5A404C5CBA6F6F80A59350DF557D9F4D?"
	private static final Pattern regexDeleteJSession = Pattern.compile("[;]jsessionid=[0-9a-zA-Z]*");
	
	public static final String target = "allfiles/book/";
	public void SayHello() {
		
//		log.info("");
//		System.out.println("处理任务开始>........");
//		generateIndexMain();
		
		generateYdkHtml();
	}
	public static void generateYdkHtml()
	{
		URL url = null;
		try {
			url = new URL("http://www.yueduke.com:8088/YdkWeb/homepage.do?errorMsg=generateHtml");
		} catch (MalformedURLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
//		URL url = createURL("http://www.baidu.com", null);
		if(url==null)
		{
			log.warn("url format is wrong");
			return;
		}
//		HttpURLConnection connection = (HttpURLConnection) url.openConnection();
		HttpURLConnection connection;
		try {
			connection = (HttpURLConnection)url.openConnection();
			connection.setConnectTimeout(20000);
//			connection.
			InputStreamReader reader = new InputStreamReader(connection.getInputStream(),"UTF-8");            
			
			
//			InputStreamReader reader = new InputStreamReader(url.openStream(),"UTF-8");
	        //System.out.println("content: "+connection.getContent().toString());
			BufferedReader br = new BufferedReader(reader);
			String result = br.readLine();
			
			String htmlFilePath = TEMPLATE_PATH + "\\ydk.html";
			log.info("模板路径＝"+htmlFilePath);
			
			File afile = new File(htmlFilePath);
			Writer out = new BufferedWriter(new OutputStreamWriter(
					new FileOutputStream(afile), "utf-8"));
			for (;result != null;result = br.readLine())
			{
				
				Matcher m = regexDeleteJSession.matcher(result);
				if (m!= null)
				{
//					result = m.replaceAll("?");
					result = m.replaceAll("");
				}
				
//				result.replaceAll(regexString, target);
//				Matcher m = regex.matcher(result);
//				if (m!= null)
//				{
//					result = m.replaceAll(target);
//				}
//				if (m.matches())
//				{
//					m.replaceAll((target));
//				}
				out.write(result);
			}
			out.flush();
			out.close();
			
			br.close();
			reader.close();
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	private static Configuration cfg = null;
	private static final String TEMPLATE_PATH = Function.getPath();
	static {
		try {
			cfg = new Configuration();
			cfg.setTemplateLoader(new FileTemplateLoader(
					new File(TEMPLATE_PATH)));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void generateIndexMain()
	{
		// 业务逻辑代码调用
		Map map = new HashMap();
		// get all type
//		WebServiceSimListParams listParams = new WebServiceSimListParams();
//		listParams.setPageSize(7);
//		listParams.setPageOffset(0);
//
//		SimWebLibraryListResult result = this.getYdkSimFrontEndService()
//				.getLibraries(listParams);
//		List list = new ArrayList();
//
//		if (result != null && result.getOs().getIsSuccessful()) {
//			List booklist = new ArrayList();
//			List book2list = new ArrayList();
//			for (YdkWebLibrary library : result.getList()) {
//
//				SimWebBookCateListResult bclist = this
//						.getYdkSimFrontEndService().getBookCategroryList(
//								library.getId(), listParams);
//				if (bclist != null && bclist.getOs().getIsSuccessful()) {
//					List categoryList = new ArrayList();
//					for (YdkWebCategory category : bclist.getList()) {
//						categoryList.add(category);
//						map.put("category", category);
//					}
//					library.setCates(categoryList);
//				}
//				list.add(library);
//
//				WebServiceSimListParams listParamsBook = new WebServiceSimListParams();
//				listParamsBook.setPageSize(5);
//				listParamsBook.setPageOffset(0);
//
//				SimWebBookListResult webbooklist = this.ydkSimFrontEndService
//						.getBookListByTagName("精品" + library.getName(),
//								listParamsBook);
//				if (webbooklist != null
//						&& webbooklist.getOs().getIsSuccessful()) {
//
//					for (YdkWebBook ywbook : webbooklist.getList()) {
//						book2list.add(webbooklist.getList().get(0));
//						break;
//
//					}
//					webbooklist.getList().remove(0);
//					for (YdkWebBook ywbook : webbooklist.getList()) {
//						booklist.add(ywbook);
//
//					}
//
//				}
//				map.put("book2list", book2list);
//				map.put("booklist", booklist);
//			}
//			map.put("libraryList", list);
//		}
//
//		// 最新文摘
//		List bookDigestsList = new ArrayList();
//		listParams.setPageSize(15);
//		SimWebDigestListResult digestListResult = this
//				.getYdkSimFrontEndService().getRecommandedBookDigests(
//						listParams);
//		if (digestListResult != null
//				&& digestListResult.getOs().getIsSuccessful()) {
//
//			for (YdkWebBookDigest bookDigests : digestListResult.getList()) {
//				bookDigestsList.add(bookDigests);
//			}
//		}
//		map.put("bookDigestsList", bookDigestsList);
//
//		// 尝鲜书架
//		WebServiceSimListParams listParams1 = new WebServiceSimListParams();
//		listParams1.setPageSize(4);
//		listParams1.setPageOffset(0);
//		SimWebBookListResult webBookListResult = this
//				.getYdkSimFrontEndService().getBookListByTagName("尝鲜书架",
//						listParams1);
//
//		map.put("position1", webBookListResult);
//
//		// 畅销书架
//		listParams1.setPageSize(3);
//		SimWebBookListResult popwebBookListResult = this
//				.getYdkSimFrontEndService().getBookListByTagName("畅销书架",
//						listParams1);
//		map.put("position2", popwebBookListResult);
//
//		// 阅读客大本营
//		listParams1.setPageSize(4);
//		List eventlist = new ArrayList();
//
//		SimWebEventListResult EventResult = this.getYdkSimFrontEndService()
//				.getEvnetsByCateName("阅读客活动大本营", listParams1);
//		if (EventResult != null && EventResult.getOs().getIsSuccessful()) {
//			for (YdkWebEvent webevent : EventResult.getList()) {
//				eventlist.add(webevent);
//			}
//			map.put("EventResult", eventlist);
//		}
//
//		// 最新连载
//		listParams1.setPageSize(8);
//		SimWebBookListResult SimWebBookListResult4 = this
//				.getYdkSimFrontEndService().getBookListByTagName("最新连载",
//						listParams1);
//		map.put("position4", SimWebBookListResult4);
//
//		// 本周新书
//		listParams1.setPageSize(15);
//		SimWebBookListResult SimWebBookListResult6 = this
//				.getYdkSimFrontEndService().GetNewBooksByDate(listParams1);
//		map.put("position6", SimWebBookListResult6);
//
//		// 精品文摘
//		listParams1.setPageSize(6);
//		SimWebDigestListResult bookDigests = this.getYdkSimFrontEndService()
//				.getRecommandedBookDigests(listParams1);
//
//		if (bookDigests != null && bookDigests.getOs().getIsSuccessful()) {
//			List bookDigestsList2 = new ArrayList();
//			for (YdkWebBookDigest bookDigests2 : bookDigests.getList()) {
//				bookDigestsList2.add(bookDigests2);
//			}
//			map.put("bookDigests", bookDigestsList2);
//		}
//
//		// 大家正在关注
//		listParams1.setPageSize(9);
//		SimWebBookCuStatListResult custatListResult = this
//				.getYdkSimFrontEndService().getBookCuStatsByFreshDate(
//						listParams1);
//		if (custatListResult != null
//				&& custatListResult.getOs().getIsSuccessful()) {
//			List cuList = new ArrayList();
//			for (YdkWebBookCuStat bookCuStat : custatListResult.getList()) {
//				YdkWebBook bookListResult = this.getYdkSimFrontEndService()
//						.getBookById(bookCuStat.getYdkBookId());
//				bookCuStat.setImageSmall(bookListResult.getImageSmall());
//				cuList.add(bookCuStat);
//			}
//			map.put("custatListResult", cuList);
//		}
//
//		// 主题阅读
//		listParams1.setPageSize(5);
//		SimWebBookTagListResult bookTagListResult = this
//				.getYdkSimFrontEndService().getRecommandedBookTag(listParams1);
//		if (bookTagListResult != null
//				&& bookTagListResult.getOs().getIsSuccessful()) {
//			List bookTagList = new ArrayList();
//			listParams1.setPageSize(1);
//			for (YdkWebBookTag bookTag : bookTagListResult.getList()) {
//
//				SimWebBookListResult bookListResult = this
//						.getYdkSimFrontEndService().getBookListByTagName(
//								bookTag.getName(), listParams1);
//				if (bookListResult != null
//						&& bookListResult.getOs().getIsSuccessful()) {
//					for (YdkWebBook book : bookListResult.getList()) {
//						// System.out.println(book.getImageLarge());
//						bookTag.setImageSmall(book.getImageSmall());
//					}
//				}
//
//				bookTagList.add(bookTag);
//			}
//			map.put("bookTagListResult", bookTagList);
//		}
//
//
//
//		String templateFilePath = TEMPLATE_PATH + "freemarker\\indexmain.ftl";
////		log.info("模板路径＝"+templateFilePath);
//		String htmlFilePath = TEMPLATE_PATH + "indexmain.jsp";
////		log.info("路径＝"+htmlFilePath);
//		templateFilePath = templateFilePath.replace("\\", "//");
//		htmlFilePath = htmlFilePath.replace("\\", "//");
//
//		try {
//			ReadFreemaker.geneHtmlFile(templateFilePath, htmlFilePath, map);
//		} catch (IOException e) {
//			e.printStackTrace();
//		} catch (TemplateException e) {
//			e.printStackTrace();
//
//		}
	}


}
